/*
* @Author: jingyu.ma
* @Date:   2017-07-12 21:15:01
* @Last Modified by:   jingyu.ma
* @Last Modified time: 2017-08-04 21:19:37
*/

import Fetch from 'common/fetch';
import { getParams } from 'common/getParams';

//获取我的需求
const getMyDemandType = (type, data) => {
    let action = {
        type:type
    }
    if(type == 'GET_MY_DEMAND_SUCCESS'){
        action['data'] = data;
    }else if(type == 'GET_MY_DEMAND_ERROR'){
        action['e'] = data;
    }
    return action;
}

const GET_MY_DEMAND_BEGIN = 'GET_MY_DEMAND_BEGIN';
const GET_MY_DEMAND_SUCCESS = 'GET_MY_DEMAND_SUCCESS';
const GET_MY_DEMAND_ERROR = 'GET_MY_DEMAND_ERROR';

export const getMyDemand = (params) => (dispatch, getState) => {
    dispatch(getMyDemandType(GET_MY_DEMAND_BEGIN));
    let myHeaders = new Headers();
    let paramsList = getParams(params);
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/demand/myDemand?'+paramsList).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(getMyDemandType(GET_MY_DEMAND_SUCCESS,data.data));
        }else{
            message.error(data.err)
            dispatch(getMyDemandType(GET_MY_DEMAND_ERROR,data));
        }
    }).catch(e=>{
        dispatch(eidtPeripheralSkill(GET_MY_DEMAND_ERROR,e));
    })
}
//获取我的技能
const getMySkillType = (type, data) => {
    let action = {
        type:type
    }
    if(type == 'GET_MY_SKILL_SUCCESS'){
        action['data'] = data;
    }else if(type == 'GET_MY_SKILL_ERROR'){
        action['e'] = data;
    }
    return action;
}

const GET_MY_SKILL_BEGIN = 'GET_MY_SKILL_BEGIN';
const GET_MY_SKILL_SUCCESS = 'GET_MY_SKILL_SUCCESS';
const GET_MY_SKILL_ERROR = 'GET_MY_SKILL_ERROR';

export const getMySkill = (params) => (dispatch, getState) => {
    dispatch(getMySkillType(GET_MY_SKILL_BEGIN))
    let myHeaders = new Headers();
    let paramsList = getParams(params);
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/skill/mySkill?'+paramsList).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(getMySkillType(GET_MY_SKILL_SUCCESS,data.data));
        }else{
            message.error(data.err)
            dispatch(getMySkillType(GET_MY_SKILL_ERROR,data));
        }
    }).catch(e=>{
        dispatch(eidtPeripheralSkill(GET_MY_SKILL_ERROR,e));
    })
}